package com.ciwong.epaper.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;

import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.epaper.bean.CatalogueInfo;
import com.ciwong.epaper.modules.epaper.bean.Module;
import com.ciwong.epaper.modules.epaper.bean.ModuleContent;
import com.ciwong.epaper.modules.epaper.bean.ZipPackageInfo;
import com.ciwong.epaper.util.download.DownLoadInfo;
import com.ciwong.epaper.utils.Constants;
import com.ciwong.mobilelib.bean.UserInfoBase;
import com.ciwong.mobilelib.utils.BaseConstants;
import com.ciwong.mobilelib.utils.BaseSystem;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 新电子报 系统类
 *
 * @author bin
 * @version ciwong v.1.0 2015/7/29 10:41
 * @since ciwong v.1.0
 */
public class ESystem extends BaseSystem {
    /**
     * 本地HTML网页目录
     */
    public static final String HTML = "html";
    /**
     * 本地测试文件夹
     */
    public static final String TEST = "test";


    /**
     * 本地HTML网页目录
     */
    public static final String HTML_SUFFIX = ".html";

    /**
     * 本地视频讲解代码
     */
    public static final String VIDEO = "video";
    public static final String ONLINE_PAPER = "onlinePaper";
    public static final String Interactive_Tutorial = "interact";
    public static final String ERROR_BOOK = "errorBook";
    public static final String Res = "res";


    //h5压缩包 报名系统
    public static final String REGIST = "signUp";

    //h5压缩包 报名系统
    public static final String NEWVIEDO = "newVideo";
    //听说模考压缩包
    public static final String LISTENSPEAK = "ListenSpeak";

    //听说模考压缩包
    public static final String LISTENSPEAKREDO = "lsReform";

    //视频配音目录
    public static final String DBUING = "dubing";

    /**
     * 答案保存目录
     */
    public static final String ANSWERS = "answers";
    /**
     * 答案保存目录需要上传
     */
    public static final String ANSWERS_NEED_UPLOAP = "nupload";

    /**
     * 答案opus保存临时目录
     */
    public static final String ANSWERS_OPUS = "tempOpus";
    /**
     * 答案保存目录句子
     */
    public static final String ANSWERS_SENTENCE = "sentence";
    /**
     * 答案保存目录单词
     */
    public static final String ANSWERS_WORD = "word";
    /**
     * 背单词书籍的保存目录
     */
    public static final String RECITE_WORD_BOOK = "reciteword";

    /**
     *
     */
    public static final String WORD_LIST = "wrodlist";

    public static final String RECORD = "record";
    public static final String NATIVE = "native";

    /**
     * 作业对应试题信息保存文件名
     */
    public static final String MAIN = "main.json";
    public static final String RESOURCE = "resource";
    public static final String CATALOGUEJSON = "catalogue.json";


    public static final String JSON_SUFFIX = ".json";
    /**
     * 试题对应参考答案保存文件名
     */
    public static final String REF_ANSWER = "refAnswer.json";

    /**
     * 试题对应用户答案保存文件名
     */
    public static final String USER_ANSWER = "userAnswer.json";

    /**
     * 答案媒体保存目录
     */
    public static final String MEDIA = "media";
    public static final String IMAGE = "image";


    /**
     * 答案上传临时解压验证的目录
     */
    public static final String HOMEWORK = "homework";


    /**
     * 分享时的图标
     */
    public static final String SHARE_ICON = "shareicon";
    private static long userId = 0;

    private static Context mContext;

    private static ESystem instance;
    /**
     * 获取唯一实例
     *
     * @return 唯一实例
     */
    public static ESystem getInstance() {
        if (instance == null) {
            syncInit();
        }
        return instance;
    }

    /**
     * 实例化对象
     */
    private static synchronized void syncInit() {
        if (instance == null) {
            instance = new ESystem();
        }
    }

    /**
     * 设置上下文对象
     *
     * @param context 上下文对象
     */
    public static void setContext(Context context) {
        mContext = context;
    }


    /**
     * 获取本地HTML目录
     *
     * @return 本地HTML目录
     */
    public static String getHtmlPath() {
        String path = getProjectFolderPath(mContext) + File.separator + HTML;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取程序公用缓存目录Music
     *
     * @return 程序缓存目录目录
     */
    public static File getMusicCacheDir(Context mContext, String filName) {
        File tempFile;
        tempFile = new File(mContext.getApplicationContext().getExternalCacheDir().getAbsolutePath(), filName);
        return tempFile;
    }


    /**
     * 获取手机拍一拍 涂鸦临时文件
     *
     * @return 图片地址
     */
    public static String getShareIconPath() {
        String path = getProjectFolderPath(mContext) + File.separator + SHARE_ICON;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 答案上传临时解压验证的目录
     *
     * @return 答案上传临时解压验证的目录
     */
    public static String getUploadUpzipHomeworkPath() {
        String path = getPackagesPath(mContext) + File.separator + HOMEWORK;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }


    /**
     * 获取本地视频讲解目录
     *
     * @return 本地HTML目录
     */
    public static String getVideoPath() {
        String path = getProjectFolderPath(mContext) + File.separator + VIDEO;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取本地线上作答目录
     *
     * @return 本地HTML目录
     */
    public static String getOnlinePaperPath() {
        String path = getProjectFolderPath(mContext) + File.separator + ONLINE_PAPER;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取互动教程目录
     *
     * @return 本地HTML目录
     */
    public static String getInteractiveTutorialPath() {
        String path = getProjectFolderPath(mContext) + File.separator + Interactive_Tutorial;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    //    /**
    //     * 获取本地线上作答目录
    //     *
    //     * @return 本地HTML目录
    //     */
    //    public static String getErrorBookPath()
    //    {
    //        String path = getProjectFolderPath() + File.separator + ERROR_BOOK;
    //        File f = new File(path);
    //        if (!f.exists())
    //        {
    //            f.mkdirs();
    //        }
    //        return path;
    //    }


    /**
     * 获取报名系统h5的目录
     *
     * @return 本地HTML目录
     */
    public static String getRegistPath() {
        String path = getProjectFolderPath(mContext) + File.separator + REGIST;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取报名系统h5的目录
     *
     * @return 本地HTML目录
     */
    public static String getVideoEXPath() {
        String path = getProjectFolderPath(mContext) + File.separator + NEWVIEDO;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取听说模考学习记录
     *
     * @return 本地HTML目录
     */
    public static String getLsWorkEXPath() {
        String path = getProjectFolderPath(mContext) + File.separator + LISTENSPEAK;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取配音根目录
     *
     * @return 本地HTML目录
     */
    public static String getDubingPath() {
        String path = getProjectFolderPath(mContext) + File.separator + DBUING;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取配音视频目录
     *
     * @return 本地HTML目录
     */
    public static String getDubingVideoPath() {
        String path = getDubingPath() + File.separator + VIDEO;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取配音录音根目录
     *
     * @return 本地HTML目录
     */
    public static String getDubingAnswersPath() {
        String path = getDubingPath() + File.separator + ANSWERS;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取配音用户录音目录
     *
     * @return 本地HTML目录
     */
    public static String getDubingAnswersUserPath() {
        String path = getDubingAnswersPath() + File.separator + getUserId();
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取配音用户录音目录(单个视频)
     *
     * @return 本地HTML目录
     */
    public static String getDubingAnswersUserVideoPath(String versionId) {
        String path = getDubingAnswersUserPath() + File.separator + versionId;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取配音用户录音目录(单个视频,单次配音)
     *
     * @return 本地HTML目录
     */
    public static String getDubingAnswersUserAudioPath(String versionId, String uuid) {
        String path = getDubingAnswersUserVideoPath(versionId) + File.separator + uuid;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取听说模考重做
     *
     * @return 本地HTML目录
     */
    public static String getLsReDoPath() {
        String path = getProjectFolderPath(mContext) + File.separator + LISTENSPEAKREDO;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }


    /**
     * 获取资源包目录名
     *
     * @param packageId 资源包ID
     * @param cId       目录ID
     * @return 资源包目录路径
     */
    public static String getPackagesCatalogue(String packageId, String cId) {
        String path = getPackagesPath(mContext) + File.separator + "catalogue_" + packageId + "_" + cId;
        return path;
    }

    /**
     * 获取资源包背单词加压资源文件名
     *
     * @param fileName 保存的文件名
     * @return 资源包目录路径
     */
    public static String getPackagesReciteWord(String fileName) {
        String path = getReciteWordBookPath() + File.separator + fileName;
        return path;
    }

    /**
     * 获取资源包目录名
     *
     * @param packageId 资源包ID
     * @param cId       目录ID
     * @return 资源包目录路径
     */
    public static String getPackagesCatalogueNew(String packageId, String cId, String moduleId, String versionId) {
        String path = getPackagesPath(mContext) + File.separator + "catalogue_" + packageId + "_" + cId + "_" + moduleId + "_" + versionId;
        if ("10".equals(moduleId) || "127".equals(moduleId)) {
            File file = new File(path);
            if (!file.exists()) {
                if (moduleId.equals("10")) {
                    path = getPackagesPath(mContext) + File.separator + "catalogue_" + packageId + "_" + cId + "_" + 127 + "_" + versionId;

                } else {
                    path = getPackagesPath(mContext) + File.separator + "catalogue_" + packageId + "_" + cId + "_" + 10 + "_" + versionId;

                }
            }
        }
        return path;
    }

    /**
     * 获取资源包Json文件路径
     *
     * @param resourcePath 资源相对路径
     * @return 资源包Json文件路径
     */
    public static String getPackagesJsonPath(String resourcePath) {
        String path = getPackagesPath(mContext) + File.separator + resourcePath;
        return path;
    }


    /**
     * 获取资源包Json文件路径
     *
     * @return 资源包Json文件路径
     */
    public static String getPackagesJsonPathNew(String packageId, String cId, String moduleId, String versionId) {
        String path = getPackagesPath(mContext) + File.separator + "catalogue_" + packageId + "_" + cId + "_" + moduleId + "_" + versionId + File.separator + RESOURCE + File.separator + RESOURCE + JSON_SUFFIX;
        if ("10".equals(moduleId) || "127".equals(moduleId)) {
            File file = new File(path);
            if (!file.exists()) {
                if (moduleId.equals("10")) {
                    path = getPackagesPath(mContext) + File.separator + "catalogue_" + packageId + "_" + cId + "_" + 127 + "_" + versionId + File.separator + RESOURCE + File.separator + RESOURCE + JSON_SUFFIX;

                } else {
                    path = getPackagesPath(mContext) + File.separator + "catalogue_" + packageId + "_" + cId + "_" + 10 + "_" + versionId + File.separator + RESOURCE + File.separator + RESOURCE + JSON_SUFFIX;


                }
            }
        }

        return path;
    }

    /**
     * 获取资源包Json文件路径
     *
     * @return 资源包Json文件路径
     */
    public static String getPackagesJsonPathNew(DownLoadInfo downLoadInfo) {
        String path = getPackagesPath(mContext) + File.separator + "catalogue_" + downLoadInfo.getBookId() + "_" + downLoadInfo.getChapterId() + "_" + downLoadInfo.getModuleId() + "_" + downLoadInfo.getVersionId() + File.separator + RESOURCE + File.separator + RESOURCE + JSON_SUFFIX;
        if ("10".equals(downLoadInfo.getModuleId()) || "127".equals(downLoadInfo.getModuleId())) {
            File file = new File(path);
            if (!file.exists()) {
                if ("10".equals(downLoadInfo.getModuleId())) {
                    path = getPackagesPath(mContext) + File.separator + "catalogue_" + downLoadInfo.getBookId() + "_" + downLoadInfo.getChapterId() + "_" + 127 + "_" + downLoadInfo.getVersionId() + File.separator + RESOURCE + File.separator + RESOURCE + JSON_SUFFIX;

                } else {
                    path = getPackagesPath(mContext) + File.separator + "catalogue_" + downLoadInfo.getBookId() + "_" + downLoadInfo.getChapterId() + "_" + 10 + "_" + downLoadInfo.getVersionId() + File.separator + RESOURCE + File.separator + RESOURCE + JSON_SUFFIX;

                }
            }
        }

        return path;
    }

    /**
     * 获取资源包Json文件路径
     *
     * @return 资源包Json文件路径
     */
    public static String getH5PackagesJsonPathNew(String packageId, String cId, String moduleId, String versionId) {
        String path = getPackagesPath(mContext) + File.separator + "catalogue_" + packageId + "_" + cId + "_" + moduleId + "_" + versionId + File.separator + RESOURCE + File.separator + RESOURCE + "_h5" + JSON_SUFFIX;
        return path;
    }

    /**
     * 获取资源包main.json
     *
     * @param packageId 资源包ID
     * @param cId       目录ID
     * @return 资源包目录内容JSON
     */
    public static String getPackagesMainJsonPath(String packageId, String cId) {
        String packagesMainJsonPath = getPackagesCatalogue(packageId, cId) + File.separator + MAIN;
        return packagesMainJsonPath;
    }

    public static String getPackagesMediaPath(String packageId, String cId) {
        String packagesMediaPath = getPackagesCatalogue(packageId, cId) + File.separator + MEDIA;
        return packagesMediaPath;
    }

    /**
     * 作业PackagesResourcePath
     *
     * @param packageId
     * @param
     * @return
     */
    public static String getPackagesResourcePath(String packageId, String cId) {
        String packagesResourcePath = getPackagesCatalogue(packageId, cId) + File.separator + RESOURCE;
        return packagesResourcePath;
    }

    public static String getPackagesResourcePath_New(String packageId, String cId, String moduleId, String versionId) {
        String packagesResourcePath = getPackagesCatalogueNew(packageId, cId, moduleId, versionId) + File.separator + RESOURCE;
        return packagesResourcePath;
    }

    public static String getPackagesMediaPath_New(String packageId, String cId, String moduleId, String versionId) {
        String packagesResourcePath = getPackagesCatalogueNew(packageId, cId, moduleId, versionId) + File.separator + MEDIA;
        return packagesResourcePath;
    }

    /**
     * 作业CatalogueJsonPath
     *
     * @param packageId
     * @param cId
     * @return
     */
    public static String getPackagesCatalogueJsonPath(String packageId, String cId) {
        String packagesCatalogueJsonPath = getPackagesCatalogue(packageId, cId) + File.separator + CATALOGUEJSON;
        return packagesCatalogueJsonPath;
    }

    /**
     * 获取json文件中的资源绝对地址
     *
     * @param resourceUrl json文件中的资源地址
     * @return
     */
    public static String formatPath(String resourceUrl) {
        if (resourceUrl != null) {
            return BaseConstants.FILE_PREFIX + getPackagesPath(mContext) + File.separator + resourceUrl;
        }
        return resourceUrl;
    }

    /**
     * 转义json字符串中的特殊字符
     *
     * @param jsonStr
     * @return
     */
    public static String formatJsonStr(String jsonStr) {
        return jsonStr/* .replaceAll("\\\\", "\\\\\\\\") */.replaceAll("'", "\\\\'");
    }

    /**
     * 获取答案保存目录
     *
     * @return 答案保存根目录
     */
    public static String getAnswersPath() {
        String path = getProjectFolderPath(mContext) + File.separator + ANSWERS;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取背单词书籍保存目录
     *
     * @return
     */
    public static String getReciteWordBookPath() {
        String path = getProjectFolderPath(mContext) + File.separator + RECITE_WORD_BOOK;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取练习册录音音频保存目录
     *
     * @return 练习册录音音频保存目录
     */
    public static String getWordListRecordPath() {
        String path = getWordListPath() + File.separator + RECORD;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取练习册原始音频保存目录
     *
     * @return 练习册原始音频保存目录
     */
    public static String getWordListNativePath() {
        String path = getWordListPath() + File.separator + NATIVE;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取练习册保存目录
     *
     * @return 练习册保存目录
     */
    public static String getWordListPath() {
        String path = getProjectFolderPath(mContext) + File.separator + WORD_LIST;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }


    /**
     * 获取答案保存目录
     *
     * @return 答案保存根目录
     */
    public static String getUploadImgPath() {
        String path = getProjectFolderPath(mContext) + File.separator + IMAGE;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取uuid目录(不创建新文件)
     *
     * @param uuid 唯一随机码
     * @return
     */
    public static String getAnswersUuidPathNoCreate(String uuid) {
        return getAnswersPath() + File.separator + uuid;
    }

    /**
     * 获取答案目录
     *
     * @return
     */
    public static String getAnswersNUploadPathCreate() {
        String path = getAnswersPath() + File.separator + ANSWERS_NEED_UPLOAP;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取opus答案目录
     *
     * @return
     */
    public static String getAnswersOpusathCreate() {
        String path = getAnswersPath() + File.separator + ANSWERS_OPUS;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取uuid目录(不创建新文件)
     *
     * @param uuid 唯一随机码
     * @return
     */
    private static String getAnswersUuidUserIdTypePathNoCreate(String uuid, String userId_type) {
        return getAnswersNUploadPathCreate() + File.separator + userId_type + File.separator + uuid;
    }

    /**
     * 获取uuid目录opus(不创建新文件)
     *
     * @param uuid 唯一随机码
     * @return
     */
    private static String getAnswersUuidUserIdTypeOpusPath(String uuid, String userId_type) {
        return getAnswersOpusathCreate() + File.separator + userId_type + File.separator + uuid;
    }

    /**
     * 获取uuid目录
     *
     * @param uuid 唯一随机码
     * @return
     */
    private static String getAnswersUserIdTypeUuidPath(String uuid, String userId_type) {
        String path = getAnswersUuidUserIdTypePathNoCreate(uuid, userId_type);
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取uuid目录Opus
     *
     * @param uuid 唯一随机码
     * @return
     */
    private static String getAnswersUserIdTypeUuidOpusPath(String uuid, String userId_type) {
        String path = getAnswersUuidUserIdTypeOpusPath(uuid, userId_type);
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    public static String getAnswersUserIdTypeUuidPathSentence(String uuid) {
        String path = getAnswersUserIdTypeUuidPath(uuid, getUserId() + ANSWERS_SENTENCE);
        return path;
    }

    public static String getAnswersUserIdTypeUuidOpusPathSentence(String uuid) {
        String path = getAnswersUserIdTypeUuidOpusPath(uuid, getUserId() + ANSWERS_SENTENCE);
        return path;
    }

    public static String getAnswersUserIdTypeUuidPathWord(String uuid) {
        String path = getAnswersUserIdTypeUuidPath(uuid, getUserId() + ANSWERS_WORD);
        return path;
    }

    public static String getAnswersUserIdTypeUuidOpusPathWord(String uuid) {
        String path = getAnswersUserIdTypeUuidOpusPath(uuid, getUserId() + ANSWERS_WORD);
        return path;
    }

    /**
     * 获取uuid目录
     *
     * @param uuid 唯一随机码
     * @return
     */
    public static String getAnswersUuidPath(String uuid) {
        String path = getAnswersUuidPathNoCreate(uuid);
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取答案对应试题信息保存文件
     *
     * @param uuid 唯一随机码
     * @return 答案对应试题信息保存文件
     */
    public static String getAnswersMainPath(String uuid) {
        String path = getAnswersUuidPath(uuid) + File.separator + MAIN;
        return path;
    }

    /**
     * 用户试题对应答案保存文件名
     *
     * @param uuid 唯一随机码
     * @return 答案保存目录
     */
    public static String getAnswersUserAnswerPath(String uuid) {
        String path = getAnswersUuidPath(uuid) + File.separator + USER_ANSWER;
        return path;
    }

    /**
     * 获取试题对应参考答案保存文件名
     *
     * @param uuid 唯一随机码
     * @return 答案保存目录
     */
    public static String getAnswersRefAnswerPath(String uuid) {
        String path = getAnswersUuidPath(uuid) + File.separator + REF_ANSWER;
        return path;
    }

    /**
     * 获取句子跟读答案媒资源保存目录
     *
     * @param uuid 唯一随机码
     * @return 答案媒体资源保存目录
     */
    public static String getAnswersUserIdMediaPathSentence(String uuid) {
        String path = getAnswersUserIdTypeUuidPathSentence(uuid) + File.separator + MEDIA;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取句子跟读答案opus资源保存目录
     *
     * @param uuid 唯一随机码
     * @return 答案媒体资源保存目录
     */
    public static String getAnswersUserIdMediaOpusPathSentence(String uuid) {
        String path = getAnswersUserIdTypeUuidOpusPathSentence(uuid) + File.separator + MEDIA;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取句子跟读答案媒资源保存目录
     *
     * @param uuid 唯一随机码
     * @return 答案媒体资源保存目录
     */
    public static String getAnswersUserIdMediaPathWord(String uuid) {
        String path = getAnswersUserIdTypeUuidPathWord(uuid) + File.separator + MEDIA;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取句子跟读答案资源opus保存目录
     *
     * @param uuid 唯一随机码
     * @return 答案媒体资源保存目录
     */
    public static String getAnswersUserIdMediaOpusPathWord(String uuid) {
        String path = getAnswersUserIdTypeUuidOpusPathWord(uuid) + File.separator + MEDIA;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    private static long getUserId() {
        if (EApplication.getInstance() != null) {
            UserInfoBase userInfoBase = EApplication.getInstance().getUserInfoBase();
            if (userInfoBase != null) {
                userId = userInfoBase.getUserId();
            }
        }
        return userId;
    }

    /**
     * 获取答案媒体资源保存目录
     *
     * @param uuid 唯一随机码
     * @return 答案媒体资源保存目录
     */
    public static String getAnswersMediaPath(String uuid) {
        String path = getAnswersUuidPath(uuid) + File.separator + MEDIA;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }


    public static String getAnswersImagesPath(String uuid) {
        String path = getAnswersUuidPath(uuid) + File.separator + IMAGE;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 根据Bitmap图片压缩：
     *
     * @param image 源图片
     * @return 压缩后的图片
     */
    public static Bitmap compressImage(Bitmap image) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        final int unit = 1024;
        if (baos.toByteArray().length / unit > unit) {
            // 判断如果图片大于1M,进行压缩避免在生成图片（BitmapFactory.decodeStream）时溢出
            baos.reset();// 重置baos即清空baos
            image.compress(Bitmap.CompressFormat.JPEG, 50, baos);// 这里压缩50%，把压缩后的数据存放到baos中
        }
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        // 开始读入图片，此时把options.inJustDecodeBounds 设回true了
        newOpts.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
        newOpts.inJustDecodeBounds = false;
        // 重新读入图片，注意此时已经把options.inJustDecodeBounds 设回false了
        newOpts.inSampleSize = getCompressibility(newOpts);
        isBm = new ByteArrayInputStream(baos.toByteArray());
        bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
        return bitmap;// 压缩好比例大小后再进行质量压缩
    }

    /**
     * 获取保存做作业记录key
     *
     * @param downLoadInfo
     * @param module
     * @param position
     * @param workId
     * @param typeKey      SerializableManager.SerializeKey.SHARE_KEY_DO_WORK_RECORD
     *                     SerializableManager.SerializeKey.SHARE_KEY_ANSWER_RECORD
     *                     SerializableManager.SerializeKey.SHARE_KEY_DO_WORK_UUID
     * @return
     */
    public static String getSaveRecordKey(DownLoadInfo downLoadInfo, Module module, int position, String workId, String typeKey) {
        return getSaveRecordKey(downLoadInfo, module, position, workId, "0", typeKey);
    }

    /**
     * 获取保存做作业记录key 区分模式
     *
     * @param downLoadInfo
     * @param module
     * @param position
     * @param workId
     * @param typeKey      SerializableManager.SerializeKey.SHARE_KEY_DO_WORK_RECORD
     *                     SerializableManager.SerializeKey.SHARE_KEY_ANSWER_RECORD
     *                     SerializableManager.SerializeKey.SHARE_KEY_DO_WORK_UUID
     * @return
     */
    public static String getSaveRecordKeywithMode(DownLoadInfo downLoadInfo, Module module, int position, String workId, String typeKey, int readmode) {
        return getSaveRecordKey(downLoadInfo, module, position, workId, "0", typeKey) + readmode;
    }


    /**
     * 获取保存做作业记录key 区分模式  和作业
     *
     * @param downLoadInfo
     * @param module
     * @param position
     * @param workId
     * @param typeKey      SerializableManager.SerializeKey.SHARE_KEY_DO_WORK_RECORD
     *                     SerializableManager.SerializeKey.SHARE_KEY_ANSWER_RECORD
     *                     SerializableManager.SerializeKey.SHARE_KEY_DO_WORK_UUID
     * @return
     */
    public static String getSaveRecordKeywithModeandUUid(DownLoadInfo downLoadInfo, Module module, int position, String workId, String typeKey, int readmode, String uuid) {
        return getSaveRecordKey(downLoadInfo, module, position, workId, "0", typeKey) + readmode + uuid;
    }


    /**
     * 获取保存做作业记录key
     *
     * @param downLoadInfo
     * @param module
     * @param position
     * @param workId
     * @param uuid
     * @param typeKey      SerializableManager.SerializeKey.SHARE_KEY_DO_WORK_RECORD
     *                     SerializableManager.SerializeKey.SHARE_KEY_ANSWER_RECORD
     *                     SerializableManager.SerializeKey.SHARE_KEY_DO_WORK_UUID
     * @return
     */
    public static String getSaveRecordKey(DownLoadInfo downLoadInfo, Module module, int position, String workId, String uuid, String typeKey) {
        String bookId = downLoadInfo == null ? "0" : parse(downLoadInfo.getBookId());
        String chapterId = downLoadInfo == null ? "0" : parse(downLoadInfo.getChapterId());
        try {
            if (downLoadInfo != null) {
                if (downLoadInfo.getType() == CatalogueInfo.PackageMode.PACKAGE_MODE_NEW) {

                    String moduleId = downLoadInfo.getModuleId();
                    String versionId = downLoadInfo.getVersionId();
                    return typeKey + "_" + bookId + "_" + chapterId + "_" + moduleId + "_" + versionId + "_" + parse(workId) + "_" + parse(uuid);

                } else {
                    ModuleContent moduleContent = null;
                    if (module != null && module.getResourceList() != null && !module.getResourceList().isEmpty() && position < module.getResourceList().size()) {
                        moduleContent = module.getResourceList().get(position);
                    }

                    String parentVersionId = moduleContent == null ? "0" : parse(moduleContent.getParentVersionId());
                    String versionId = moduleContent == null ? "0" : parse(moduleContent.getVersionId());
                    return typeKey + "_" + bookId + "_" + chapterId + "_" + parentVersionId + "_" + versionId + "_" + parse(workId) + "_" + parse(uuid);
                }
            }
        } catch (Exception e) {
            e.getStackTrace();
        }


        return "";


    }


    /**
     * 获取保存做作业记录key
     *
     * @param workId
     * @param uuid
     * @param typeKey SerializableManager.SerializeKey.SHARE_KEY_DO_WORK_RECORD
     *                SerializableManager.SerializeKey.SHARE_KEY_ANSWER_RECORD
     *                SerializableManager.SerializeKey.SHARE_KEY_DO_WORK_UUID
     * @return
     */
    public static String getSaveRecordKeyFromKey(String bookId, String chapterId, String parentVersionId, String versionId, String workId, String uuid, String typeKey) {

        return typeKey + "_" + bookId + "_" + chapterId + "_" + parentVersionId + "_" + versionId + "_" + parse(workId) + "_" + parse(uuid);
    }

    /**
     * 为空时转化为0
     *
     * @param id
     * @return
     */
    public static String parse(String id) {
        return TextUtils.isEmpty(id) ? "0" : id;
    }

    /**
     * 获得尺寸压缩比例
     *
     * @param options 图片的属性
     * @return 尺寸压缩比例
     */
    private static int getCompressibility(BitmapFactory.Options options) {
        int be = 1;
        int width = options.outWidth;
        int height = options.outHeight;

        final int size = 500;
        if (height > size || width > size) {
            float wBe = width / size;
            float hBe = height / size;
            if (wBe > hBe) {
                be = (int) wBe + 1;
            } else {
                be = (int) hBe + 1;
            }
        }
        if (be <= 0) {
            be = 1;
        }
        return be;
    }

    /**
     * 判断当前是否还存在mp3文件有可能出现被第三方删除了资源
     * @param
     * @return
     */
    //    public static boolean isExistsMp3(String packId,String conId){
    //        Log.d("xixin","----------isExistsMp3------");
    //        try{
    //            if(packList!=null){
    //                if(packList.contains(packId+conId)){
    //                    //如果已经下载过了然而media目录下面还是没这个那就不下载了
    //                    Log.d("xixin","----------isExistsMp3-contains-----");
    //                    return true;
    //                }
    //            }
    //            String caPath = ESystem.getPackagesCatalogue(
    //                    packId, conId);
    //            File tempFile=new File(caPath+"/media");
    //            if(tempFile.exists()){
    //                if(tempFile.listFiles()!=null){
    //                    for(File fi:tempFile.listFiles()){
    //                        //判断是否存在mp文件
    //                        if(fi.getName().endsWith(".mp3")||fi.getName().endsWith(".mp4")){
    //                            Log.d("xixin","----------isExistsMp3-mp3-----");
    //                            return true;
    //                        }
    //                    }
    //                }
    //            }else{
    //                //如果跟目录都不存说明之前都没下载过根本无需做任何操作
    //                return true;
    //            }
    //            if(packList==null){
    //                packList=new ArrayList<String>();
    //            }
    //            packList.add(packId+conId);
    //            String jsonPath = ESystem.getPackagesMainJsonPath(
    //                    packId, conId);
    //            File deleteFile=new File(jsonPath);
    //            if(deleteFile.exists()){
    //                deleteFile.delete();
    //            }
    //            return false;
    //        }catch (Exception e){
    //            e.printStackTrace();
    //            return true;
    //        }
    //    }

    /**
     * 该集合专门用于存储下载过的包资源
     */
    public static List<String> packList;

    /**
     * 递归删除目录
     *
     * @param f
     */
    public static void recurDelete(File f) {
        try {
            for (File fi : f.listFiles()) {
                if (fi.isDirectory()) {
                    recurDelete(fi);
                } else {
                    fi.delete();
                }
            }
            f.delete();
        } catch (NullPointerException n) {
            System.out.println("Sorry,No such file");
        }
    }

    public static String getUnZipTargetPath(ZipPackageInfo zipPackageInfo) {
        String targetPath;
        if ("ListenSpeak".equals(zipPackageInfo.getRepoName()) || "听说模考".equals(zipPackageInfo.getRepoSummary())) {
            targetPath = ESystem.getProjectFolderPath(mContext);
        } else {
            targetPath = ESystem.getProjectFolderPath(mContext) + File.separator + zipPackageInfo.getRepoName();

        }
        return targetPath;
    }

    public static String getDeleteTargetPath(ZipPackageInfo zipPackageInfo) {
        String targetPath;
        if ("ListenSpeak".equals(zipPackageInfo.getRepoName()) || "听说模考".equals(zipPackageInfo.getRepoSummary())) {
            targetPath = ESystem.getProjectFolderPath(mContext) + File.separator + ESystem.LISTENSPEAKREDO;
        } else {
            targetPath = ESystem.getProjectFolderPath(mContext) + File.separator + zipPackageInfo.getRepoName();

        }
        return targetPath;
    }

    /**
     * 获取程序私有缓存目录目录
     *
     * @return 程序缓存目录目录
     */
    public static File getCacheDir(Context mContext, String filName) {
        File tempFile = new File(mContext.getApplicationContext().getExternalCacheDir().getAbsolutePath(), filName);
        return tempFile;
    }

    /**
     * 获取程序公用缓存目录目录
     *
     * @return 程序缓存目录目录
     */
    public static File getPicturesCacheDir(Context mContext, String filName) {
        File tempFile;
//        File tempFile = new File(mContext.getApplicationContext().getExternalCacheDir().getAbsolutePath(), filName);
//        if (Build.VERSION.SDK_INT >= 30) {
//            //android 11以上，将文件创建在公有目录
//            String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath();
//            //storage/emulated/0/Pictures
//            tempFile = new File(path, filName);
//        } else {
            tempFile = new File(mContext.getApplicationContext().getExternalCacheDir().getAbsolutePath(), filName);
//        }
        return tempFile;
    }

    /**
     * 获取address.db程序缓存目录目录
     *
     * @return 程序缓存目录目录
     */
    public static File getCacheAddressFile(Context mContext) {
        String cachePath = mContext.getApplicationContext().getExternalCacheDir().getAbsolutePath();
        File tempFile = new File(cachePath, Constants.ADDRESS_DB);
        return tempFile;
    }
    /**
     * 使用系统当前日期加以调整作为临时文件的名称
     *
     * @param identifying 标识
     * @param suffix      后缀
     * @return
     */
    public static String getCacheFileName(String identifying, String suffix) {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "'t'_yyyyMMdd_HHmmss");
        return identifying + dateFormat.format(date) + "." + suffix;
    }
}
